$OpenBSD: patch-xbsndsrv_c,v 1.4 2009/12/20 00:14:40 jakemsr Exp $
--- xbsndsrv.c.orig	Mon Jun 12 04:06:36 2006
+++ xbsndsrv.c	Wed Dec 16 19:48:41 2009
@@ -75,6 +75,10 @@
 #ifdef HAVE_LINUX_SOUNDCARD_H
 #include <linux/soundcard.h>
 #endif
+#ifdef __OpenBSD__
+#define USE_SNDIO
+#include <sndio.h>
+#endif
 
 #endif
 
@@ -263,6 +267,10 @@ static struct _sound_name
 #define SAMPLE_CHANNELS     2
 #define SAMPLE_SIZE         16
 #define SUN_AUDIO_REF 0
+#elif defined(USE_SNDIO)
+#define SOUND_DEVICE "default"
+#define SAMPLE_CHANNELS     1
+#define SAMPLE_SIZE         8
 #else
 #define SOUND_DEVICE "/dev/dsp"
 #define SAMPLE_CHANNELS     1
@@ -283,6 +291,10 @@ static int sample_rate = SAMPLE_RATE;
 static int sample_channels = SAMPLE_CHANNELS;
 static int sample_size = SAMPLE_SIZE;
 
+#ifdef USE_SNDIO
+struct sio_hdl *hdl;
+#endif
+
 /*
  * outcomment the following line to suppress server statistics
  */
@@ -371,8 +383,20 @@ init_dsp (dsp)
 	fprintf (stderr, "  (wanted %d channels, %d bits, %dHz)\n",
 			 sample_channels, sample_size, sample_rate);
 
-#else
+#elif defined(USE_SNDIO)
+	struct sio_par par;
 
+	sio_initpar(&par);
+	par.rate = sample_rate;
+	par.bits = sample_size;
+	par.sig = par.bits == 8 ? 0 : 1;
+	par.pchan = sample_channels ? 2 : 1;
+	par.appbufsz = 1024;
+
+	if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par) || !sio_start(hdl))
+		fprintf (stderr, "XBlast sound server: could not configure sndio\n");
+
+#else
 	if (ioctl (dsp, SNDCTL_DSP_SETFRAGMENT, &fragsize) < 0) {
 		fprintf (stderr, "XBlast sound server: could not set fragment size %8x on sound device\n",
 				 fragsize);
@@ -418,7 +442,7 @@ resync (dsp)
 #ifdef __sun__
 	ioctl (dsp, I_FLUSH, NULL);
 	fprintf (stderr, "\nsync\n");
-#else
+#elif !defined(USE_SNDIO)
 	/* resync sound device to correct any channel flipping */
 	write (dsp, playbuff, SUBSIZE);
 	write (dsp, playbuff, SUBSIZE);
@@ -601,7 +625,11 @@ main (argc, argv)
 	/*
 	 * open and prepare sound device
 	 */
+#ifdef USE_SNDIO
+	if ((hdl = sio_open (NULL, SIO_PLAY, 0)) == NULL) {
+#else
 	if ((dsp = open (SOUND_DEVICE, O_WRONLY)) < 0) {
+#endif
 		fprintf (stderr, "XBlast sound server: could not open sound device %s\n", SOUND_DEVICE);
 		ack_val = SND_ACK_ERROR;
 		write (1, &ack_val, sizeof (ack_val));
@@ -780,7 +808,11 @@ main (argc, argv)
 				}
 
 				/* play buffer */
+#ifdef USE_SNDIO
+				sio_write (hdl, playbuff, SUBSIZE);
+#else
 				write (dsp, playbuff, SUBSIZE);
+#endif
 			}
 		}
 		else {
@@ -928,6 +960,15 @@ main (argc, argv)
 					}
 				}
 
+#elif defined(USE_SNDIO)
+				switch (sample_size) {
+				case 8:
+					sio_write (hdl, playbuff, SUBSIZE);
+					break;
+				case 16:
+					sio_write (hdl, sumbuff, SUBSIZE * 2);
+					break;
+				}
 #else
 				switch (sample_size) {
 				case 8:
@@ -950,7 +991,7 @@ main (argc, argv)
 			/* ioctl(dsp,AUDIO_DRAIN,NULL); */
 			ioctl (dsp, I_FLUSH, NULL);
 			fprintf (stderr, "\nsync\n");
-#else
+#elif !defined(USE_SNDIO)
 			(void)ioctl (dsp, SNDCTL_DSP_SYNC, NULL);
 #endif
 			did_sync = TRUE;
